home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0083_Fractals!.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  4KB  |  115 lines

  1. {
  2. For all of you who are interested on fractals, here is a little program,
  3. taken from a source code in Modula-2, that will draw a Mandelbrot fractal.
  4.  
  5. Just one problem: If your computer doesn't have a math coprocessor, the
  6. program will run "a bit" slow :).
  7.  
  8. Try modifying all the constants, you'll get strange results :).
  9. }
  10. {$N+}
  11. {$X+ Enable Extended Syntax                                       }
  12. Program Mandelbrot;     {Using real numbers. For TP 6.0 and above }
  13.  
  14. Uses Crt;               {Only to use "ReadKey" Function.          }
  15.  
  16. Const Colours=255;       {Number of colors to be on the image.     }
  17.       Width=320;        {Width of the image.                      }
  18.       Height=200;       {Height of the image.                     }
  19.       Limit=8.0;        {Until when we calculate.                 }
  20.       XRMin=-2.0;       {Left limit of the fractal.               }
  21.       XRMax=1.0;        {Right limit of the fractal.              }
  22.       YRMin=-1.3;       {Lower limit of the fractal.              }
  23.       YRMax=1.3;        {Upper limit of the fractal.              }
  24.  
  25. Type Palette=Array[0..767] of Byte;  {MCGA/VGA palette type       }
  26.  
  27. Var XPos,YPos:Word;
  28.  
  29. {Sets the desired video mode (13h)                                }
  30. Procedure SetVideoMode(VideoMode:Byte); Assembler;
  31. Asm
  32.   xor ax,ax                 {BIOS Function 00h: Set Video Mode.   }
  33.   mov al,VideoMode          {Desired Video Mode.                  }
  34.   int 10h
  35. End;
  36.  
  37. {Creates a palette: Black --> red --> yellow                      }
  38. Procedure MakePalette;
  39. Var CPal:Palette;
  40.     i:Byte;
  41.  
  42.   {Sets the palette.                                              }
  43.   Procedure SetPalette(Pal:Palette); Assembler;
  44.   Asm
  45.     push es
  46.     mov ax,1012h            {BIOS function 10h, subfunction 12h.  }
  47.     xor bx,bx               {first color register.                }
  48.     mov cx,20h              {number of color registers.           }
  49.     les dx,Pal              {ES:DX Segment:Offset of color table. }
  50.     Int 10h
  51.     pop es
  52.   End;
  53.  
  54. Begin
  55.   For i:=0 to 15 do
  56.   Begin
  57.     CPal[3*i]:=4*i+3; CPal[3*i+1]:=0; CPal[3*i+2]:=0;
  58.     CPal[3*i+48]:=63; CPal[3*i+49]:=4*i+3; CPal[3*i+50]:=0;
  59.   End;
  60.   SetPalette(CPal);
  61. End;
  62.  
  63. {Draws a Plot of the desired color on screen.                     }
  64. Procedure DrawPixel(XPos,YPos:Word; PlotColour:Byte);
  65. Begin
  66.   Mem[$A000:YPos*320+XPos]:=PlotColour;
  67. End;
  68.  
  69. {Needs to be explained? ;-)                                       }
  70. Procedure Beep;
  71. Begin
  72.   Sound(3000); Delay(90); Sound(2500); Delay(90);
  73.   NoSound;
  74. End;
  75.  
  76. {Calculates the color for each point.                             }
  77. Function ComputeColour(XPos,YPos:Word):Byte;
  78. Var RealP,ImagP:Real;
  79.     CurrX,CurrY:Real;
  80.     a2,b2:Real;
  81.     Counter:Byte;
  82.  
  83. Begin
  84. CurrX:=XPos/Width*(XRMax-XRMin)+XRMin;
  85.   CurrY:=YPos/Height*(YRMax-YRMin)+YRMin;
  86.   RealP:=0;
  87.   ImagP:=0;
  88.   Counter:=0;
  89.   Repeat
  90.     a2:=Sqr(RealP);
  91.     b2:=Sqr(ImagP);
  92.     ImagP:=2*RealP*ImagP+CurrY;
  93.     RealP:=a2-b2+CurrX;
  94.     Inc(Counter);
  95.   Until (Counter>=Colours) or (a2+b2>=Limit);
  96.   ComputeColour:=Counter-1;
  97. End;
  98.  
  99. Begin
  100.   Writeln('Program to draw Fractals of Mandelbrot.');
  101.   Writeln('Written by Miguel Martínez. ');
  102.   Writeln('Press any key to continue...');
  103.   If ReadKey=#0 Then ReadKey;   {Skip double codes.               }
  104.  
  105.   SetVideoMode(19);             {Set 320x200x256 graphics mode.   }
  106.   MakePalette;
  107.   For YPos:=0 to (Height-1) do
  108. For XPos:=0 to (Width-1) do
  109.       DrawPixel(XPos,YPos,ComputeColour(XPos,YPos));
  110.   Beep;                         {Beep when finished.              }
  111.   If ReadKey=#0 Then ReadKey;
  112.   ReadKey;
  113.   SetVideoMode(3);              {Restore text mode.               }
  114. End.
  115.